package com.deimos.tests;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.vast.ows.sps.DescribeResultAccessResponse;
import org.vast.ows.sps.SPSUtils;
import org.vast.ows.sps.StatusReport;
import org.vast.xml.DOMHelper;

import com.deimos.HMA;
import com.deimos.UtilsDeimos;
import com.deimos.dataBase.TaskController;

public class DescribeResultAccess{

    private static HMA hma = new HMA();
    SPSUtils utils = new SPSUtils();
    static String taskId = null;

    @BeforeClass
    public static void init() {
	TaskController taskController = new TaskController();
	taskController.createTables(false);
    }


    @Before
    public void describeResultAccess_Step1() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "DescribeResultAccess_Task_Exists_Step1_Sumit_Request.xml"));
	response = hma.Submit(request);
	
	StatusReport report = UtilsDeimos.readParams(response.toString()).getReport();
	if (report.getDescription().contains(" is in use by the task ")){
	    taskId = report.getDescription().split(" is in use by the task ")[1];  
	    describeResultAccess_Step3();
	    taskId = null;
	    describeResultAccess_Step1();
	}
	
	if (taskId == null) taskId = UtilsDeimos.readParams(response.toString()).getReport().getTaskID();
	assertTrue(taskId != null);
    }

    @Test
    public void describeResultAccess_Task_Exists() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "DescribeResultAccess_Task_Exists_Step2_DescribeResultAccess_Request.xml").replace("TASKID", taskId));
	response = hma.DescribeResultAccess(request);
	InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	DOMHelper domResponse = new DOMHelper(is, false);
	assertTrue(((DescribeResultAccessResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "DescribeResultAccessResponse", "2.0")).getResultGroups().size() != 0);
    }
    
    @Test
    public void describeResultAccess_Task_Not_Found() throws Exception {
	OMElement request, response;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "DescribeResultAccess_Task_Not_Found_Request.xml"));
	response = hma.DescribeResultAccess(request);
	InputStream is = new ByteArrayInputStream(response.toString().getBytes());
	DOMHelper domResponse = new DOMHelper(is, false);
	assertTrue(((DescribeResultAccessResponse) utils.readXMLResponse(domResponse, domResponse.getRootElement(), "SPS", "DescribeResultAccessResponse", "2.0")).getReasonCode().compareTo("Data currently unavailable") == 0);
    }
    
    @After
    public void describeResultAccess_Step3() throws Exception {
	OMElement request;
	request = AXIOMUtil.stringToOM(AllTests.readTest(AllTests.test_dir + "DescribeResultAccess_Task_Exists_Step3_Cancel_Request.xml").replace("TASKID", taskId));
	hma.Cancel(request);
    }
    
}
